﻿using AntdUI;
using Super.Admin.Utils.EventAggregator;
using Super.Admin.Utils.EventAggregator.EventType;
using Super.Admin.Utils.ThemeManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Super.Admin.Views.Main
{
    public partial class UcMainDrawerSettingAppearanceSetting : UserControl
    {
        private bool _isLoad = false;
        public UcMainDrawerSettingAppearanceSetting()
        {
            InitializeComponent();
            this.Load += UcMainDrawerSettingAppearanceSetting_Load;
        }

        private void UcMainDrawerSettingAppearanceSetting_Load(object? sender, EventArgs e)
        {
            _isLoad = true;
            // 初始化本地参数
            InitializLoadConfig();
            _isLoad = false;
        }

        private void InitializLoadConfig()
        {
            // 判断本地的主题颜色是什么
            if (ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.Theme.ThemeType == AntdUI.TAMode.Light)
            {
                BtnSwitchTheme.SelectIndex = 0; // 浅色主题
            }
            else
            {
                BtnSwitchTheme.SelectIndex = 1; // 深色主题
            }
            // 判断侧边栏深色模式
            SwitchDarkSidebar.Checked = ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.Theme.DarkSidebar;
            // 判断顶栏深色模式
            SwitchDarkTopBar.Checked = ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.Theme.DarkTopBar;
            ColorPrimaryColor.Value = ColorTranslator.FromHtml(ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.BuiltInThemes.PrimaryColor); // 初始化颜色
            ColorWaringColor.Value = ColorTranslator.FromHtml(ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.BuiltInThemes.WarningColor); // 初始化颜色
            ColorInfoColor.Value = ColorTranslator.FromHtml(ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.BuiltInThemes.InfoColor); // 初始化颜色
            ColorSuccessColor.Value = ColorTranslator.FromHtml(ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.BuiltInThemes.SuccessColor); // 初始化颜色
            ColorErrorColor.Value = ColorTranslator.FromHtml(ThemeSettingHelper.Instance.ThemeConfig.AppearanceSettings.BuiltInThemes.ErrorColor); // 初始化颜色
        }

        // 切换主题
        private void BtnSwith_SelectIndexChanged(object sender, AntdUI.IntEventArgs e)
        {
            if (_isLoad) return;
            Segmented segmented = (Segmented)sender;
            switch (segmented.Name)
            {
                case "BtnSwitchTheme":
                    AntdUI.TAMode model = AntdUI.TAMode.Light;
                    if (e.Value == 1) model = AntdUI.TAMode.Dark;
                    EventAggregator.Default.Publish<AppearanceSettingEvent>(new AppearanceSettingEvent { Type = AppearanceSettingEvent.AppearanceSettingType.ThemeType, Value = model });
                    break;
                case "BtnSwitchCornerRadius":
                    // 切换圆角
                    EventAggregator.Default.Publish<AppearanceSettingEvent>(new AppearanceSettingEvent { Type = AppearanceSettingEvent.AppearanceSettingType.ResetPreferenceSettings, Value = e.Value * 5 });
                    break;
            }
        }
        // 切换侧边栏深色模式
        private void Switch_CheckedChanged(object sender, AntdUI.BoolEventArgs e)
        {
            if (_isLoad) return;
            Switch @switch = (Switch)sender;
            switch (@switch.Name)
            {
                case "SwitchDarkSidebar":
                    EventAggregator.Default.Publish<AppearanceSettingEvent>(new AppearanceSettingEvent { Type = AppearanceSettingEvent.AppearanceSettingType.DarkSidebar, Value = e.Value });
                    break;
                case "SwitchDarkTopBar":
                    EventAggregator.Default.Publish<AppearanceSettingEvent>(new AppearanceSettingEvent { Type = AppearanceSettingEvent.AppearanceSettingType.DarkTopBar, Value = e.Value });
                    break;
            }
        }

        private void Color_ValueChanged(object sender, ColorEventArgs e)
        {
            if (_isLoad) return;
            ColorPicker colorPicker = (ColorPicker)sender;
            switch (colorPicker.Name)
            {
                case "ColorPrimaryColor":
                    EventAggregator.Default.Publish<AppearanceSettingEvent>(new AppearanceSettingEvent { Type = AppearanceSettingEvent.AppearanceSettingType.PrimaryColor, Value = e.Value.Name });
                    break;
                case "ColorWaringColor":
                    EventAggregator.Default.Publish<AppearanceSettingEvent>(new AppearanceSettingEvent { Type = AppearanceSettingEvent.AppearanceSettingType.WarningColor, Value = e.Value.Name });
                    break;
                case "ColorInfoColor":
                    EventAggregator.Default.Publish<AppearanceSettingEvent>(new AppearanceSettingEvent { Type = AppearanceSettingEvent.AppearanceSettingType.InfoColor, Value = e.Value.Name });
                    break;
                case "ColorErrorColor":
                    EventAggregator.Default.Publish<AppearanceSettingEvent>(new AppearanceSettingEvent { Type = AppearanceSettingEvent.AppearanceSettingType.ErrorColor, Value = e.Value.Name });
                    break;
            }
        }
    }
}
